```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Bean 生命周期详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f8fafc;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-header {
            background-color: #1e1e1e;
            padding: 8px 16px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #444;
        }
        .lifecycle-card {
            transition: all 0.3s ease;
            border-left: 4px solid transparent;
        }
        .lifecycle-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
            border-left-color: #667eea;
        }
        .step-icon {
            background-color: #667eea;
            color: white;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            margin-right: 16px;
            flex-shrink: 0;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">深入理解Spring Bean生命周期</h1>
                    <p class="text-xl mb-8 opacity-90">探索IoC容器如何管理Bean从创建到销毁的全过程</p>
                    <div class="flex items-center">
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full mr-3"><i class="fas fa-code mr-2"></i>Spring Framework</span>
                        <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full"><i class="fas fa-clock mr-2"></i>高级主题</span>
                    </div>
                </div>
                <div class="md:w-1/2 mt-10 md:mt-0 flex justify-center">
                    <div class="bg-white bg-opacity-20 p-6 rounded-2xl shadow-xl" style="max-width: 300px;">
                        <div class="mermaid">
                            graph TD
                                A[实例化] --> B[属性赋值]
                                B --> C[初始化]
                                C --> D[使用中]
                                D --> E[销毁]
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Content Section -->
    <div class="container mx-auto max-w-6xl px-4 py-12">
        <!-- What is Bean -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-coffee"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">什么是Bean</h2>
                </div>
                <div class="prose max-w-none text-gray-700">
                    <p>在Spring中，由Spring IoC容器管理的构成应用程序主干的对象称为bean。bean是由Spring IoC容器实例化、组装和管理的对象。否则，bean只是应用程序中的众多对象之一。bean及其之间的依赖关系反映在容器使用的配置元数据中，通俗来讲Bean就是我们写的Java类。</p>
                </div>
            </div>
        </section>

        <!-- Normal Object Lifecycle -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">普通对象(Bean)的生命周期</h2>
                </div>
                <div class="mb-8">
                    <ul class="list-disc pl-6 space-y-2 text-gray-700">
                        <li>实例化</li>
                        <li>该对象不再被使用时通过垃圾回收机制进行回收</li>
                    </ul>
                </div>
                <div class="code-block mb-6">
                    <div class="code-header">
                        <span class="text-gray-400 text-sm">Java</span>
                        <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                    </div>
                    <pre class="p-4 overflow-x-auto"><code>public static void main(String[] args) {
    // 1.实例化
    User user = new User();
 
    // 2、使用对象
    user.setName("toString");
    user.getName();
    
    // 3.user对象不再使用就会被JVM回收
    user = null;
}</code></pre>
                </div>
                <div class="text-center">
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1644221695045-3f977dbd-f885-4948-bf10-019d24d065c2.png" alt="JVM内存图" class="rounded-lg shadow-md mx-auto max-w-full h-auto">
                </div>
            </div>
        </section>

        <!-- Spring Bean Lifecycle -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-project-diagram"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">Spring Bean的生命周期</h2>
                </div>
                <div class="prose max-w-none mb-8 text-gray-700">
                    <p>对于普通的 Java 对象，当 new 的时候创建对象，然后该对象就能够使用了。一旦该对象不再被使用，则由 Java 自动进行垃圾回收。</p>
                    <p>而 Spring 中的对象是 bean，bean 和普通的 Java 对象没啥大的区别，只不过 Spring 不再自己去 new 对象了，而是由 <strong>IoC 容器去帮助我们实例化对象并且管理它</strong>，我们需要哪个对象，去问 IoC 容器要即可。IoC 其实就是解决对象之间的耦合问题，Spring Bean 的生命周期完全由容器控制。</p>
                    <p><strong>这里我们说的 Spring Bean 的生命周期主要指的是 singleton bean，对于 prototype 的 bean ，Spring 在创建好交给使用者之后则不会再管理后续的生命周期。</strong></p>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="lifecycle-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                        <div class="flex items-start mb-4">
                            <div class="bg-blue-100 text-blue-600 rounded-full p-2 mr-3">
                                <i class="fas fa-cube"></i>
                            </div>
                            <div>
                                <h3 class="font-bold text-lg mb-1">实例化</h3>
                                <p class="text-gray-600 text-sm">通过调用构造函数或工厂方法创建Bean的实例</p>
                            </div>
                        </div>
                    </div>
                    <div class="lifecycle-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                        <div class="flex items-start mb-4">
                            <div class="bg-purple-100 text-purple-600 rounded-full p-2 mr-3">
                                <i class="fas fa-pencil-alt"></i>
                            </div>
                            <div>
                                <h3 class="font-bold text-lg mb-1">属性赋值</h3>
                                <p class="text-gray-600 text-sm">将配置文件中或通过注解设置的属性值注入到Bean实例中</p>
                            </div>
                        </div>
                    </div>
                    <div class="lifecycle-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                        <div class="flex items-start mb-4">
                            <div class="bg-green-100 text-green-600 rounded-full p-2 mr-3">
                                <i class="fas fa-play"></i>
                            </div>
                            <div>
                                <h3 class="font-bold text-lg mb-1">初始化</h3>
                                <p class="text-gray-600 text-sm">调用Bean的初始化方法（@PostConstruct或InitializingBean）</p>
                            </div>
                        </div>
                    </div>
                    <div class="lifecycle-card bg-white border border-gray-200 rounded-lg p-6 shadow-sm">
                        <div class="flex items-start mb-4">
                            <div class="bg-red-100 text-red-600 rounded-full p-2 mr-3">
                                <i class="fas fa-power-off"></i>
                            </div>
                            <div>
                                <h3 class="font-bold text-lg mb-1">销毁</h3>
                                <p class="text-gray-600 text-sm">当应用程序关闭时调用Bean的销毁方法进行清理工作</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="code-block mb-6">
                    <div class="code-header">
                        <span class="text-gray-400 text-sm">Java</span>
                        <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                    </div>
                    <pre class="p-4 overflow-x-auto"><code>@Component
public class ExampleBean implements InitializingBean, DisposableBean {
    // 构造函数
    public ExampleBean() {
        System.out.println("Bean实例化");
    }

    // 属性注入
    @Value("Example Value")
    private String property;

    // 初始化方法
    @PostConstruct
    public void init() {
        System.out.println("Bean初始化");
    }

    // 实现InitializingBean接口的方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Bean属性设置完成");
    }

    // 使用Bean
    public void doSomething() {
        System.out.println("Bean被使用");
    }

    // 销毁方法
    @PreDestroy
    public void destroy() {
        System.out.println("Bean销毁");
    }

    // 实现DisposableBean接口的方法
    @Override
    public void destroy() throws Exception {
        System.out.println("Bean销毁");
    }
}</code></pre>
                </div>
                
                <div class="text-center mb-8">
                    <img src="https://cdn.nlark.com/yuque/0/2023/png/21449790/1681292515303-3c089ed3-0d55-4c8d-9c54-7c354781567f.png" alt="Spring Bean初始化流程" class="rounded-lg shadow-md mx-auto max-w-full h-auto">
                </div>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-gray-800">初始化流程源码</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto text-sm"><code>// AbstractAutowireCapableBeanFactory.java
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
    // 1.实例化
    instanceWrapper = createBeanInstance(beanName, mbd, args);
    
    // 2.赋值(DI)
    populateBean(beanName, mbd, instanceWrapper);
    
    // 3.初始化
    exposedObject = initializeBean(beanName, exposedObject, mbd);
    
    return exposedObject;
}</code></pre>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-gray-800">容器关闭方法</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto text-sm"><code>public void close() {
    synchronized (this.startupShutdownMonitor) {
        doClose();
        if (this.shutdownHook != null) {
            try {
                Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
            }
            catch (IllegalStateException ex) {
                // ignore - VM is already shutting down
            }
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Lifecycle Functions Classification -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">SpringBean生命周期函数分类</h2>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="lifecycle-card bg-gradient-to-br from-blue-50 to-blue-100 border border-blue-200 rounded-lg p-6">
                        <h3 class="font-bold text-xl mb-3 text-blue-800">1. Bean自身的方法</h3>
                        <p class="text-gray-700">这个包括了Bean本身调用的方法和通过配置文件中&lt;bean&gt;的<strong>init-method</strong>和<strong>destroy-method</strong>指定的方法</p>
                    </div>
                    
                    <div class="lifecycle-card bg-gradient-to-br from-purple-50 to-purple-100 border border-purple-200 rounded-lg p-6">
                        <h3 class="font-bold text-xl mb-3 text-purple-800">2. Bean级生命周期接口方法</h3>
                        <p class="text-gray-700">这个包括了<strong>BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean</strong>这些接口的方法</p>
                    </div>
                    
                    <div class="lifecycle-card bg-gradient-to-br from-green-50 to-green-100 border border-green-200 rounded-lg p-6">
                        <h3 class="font-bold text-xl mb-3 text-green-800">3. 容器生命周期方法</h3>
                        <p class="text-gray-700">这个包括了<strong>InstantiationAwareBeanPostProcessor 和 BeanPostProcessor</strong>这两个接口实现，一般称它们的实现类为"后处理器"。</p>
                    </div>
                    
                    <div class="lifecycle-card bg-gradient-to-br from-yellow-50 to-yellow-100 border border-yellow-200 rounded-lg p-6">
                        <h3 class="font-bold text-xl mb-3 text-yellow-800">4. 工厂后处理器接口方法</h3>
                        <p class="text-gray-700">这个包括了<strong>AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer</strong>等等非常有用的工厂后处理器接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Demonstration -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-laptop-code"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">代码演示生命周期</h2>
                </div>
                
                <div class="space-y-12">
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">1. Bean自身的方法和Bean级生命周期接口方法</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>package com.ts.scope;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Person implements BeanFactoryAware, BeanNameAware,
        InitializingBean, DisposableBean {

    private String name;
    private String address;
    private int phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】注入属性address");
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("【注入属性】注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person [address=" + address + ", name=" + name + ", phone="
                + phone + "]";
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = arg0;
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String arg0) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
        this.beanName = arg0;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
    }

    // 这是DiposibleBean接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
    }

    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destroy-method属性指定的初始化方法
    public void myDestory() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">2. BeanPostProcessor接口的方法</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>package com.ts.scope;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    public MyBeanPostProcessor() {
        super();
        System.out.println("这是BeanPostProcessor实现类构造器！！");
    }

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改！");
        return arg0;
    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改！");
        return arg0;
    }
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">3. InstantiationAwareBeanPostProcessorAdapter 接口方法</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>package com.ts.scope;

import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

public class MyInstantiationAwareBeanPostProcessor extends
        InstantiationAwareBeanPostProcessorAdapter {

    public MyInstantiationAwareBeanPostProcessor() {
        super();
        System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器！！");
    }

    // 接口方法、实例化Bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class beanClass,
            String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
        return null;
    }

    // 接口方法、实例化Bean之后调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
        return bean;
    }

    // 接口方法、设置某个属性时调用
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs,
            PropertyDescriptor[] pds, Object bean, String beanName)
            throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
        return pvs;
    }
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">4. 后置处理器接口方法</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>package springBeanTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
        super();
        System.out.println("这是BeanFactoryPostProcessor实现类构造器！！");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
            throws BeansException {
        System.out.println("BeanFactoryPostProcessor调用postProcessBeanFactory方法");
        BeanDefinition bd = arg0.getBeanDefinition("person");
        bd.getPropertyValues().addPropertyValue("phone", "110");
    }
}</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">5. 配置文件 beans.xml</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">XML</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"&gt;

    &lt;bean id="beanPostProcessor" class="springBeanTest.MyBeanPostProcessor"&gt;
    &lt;/bean&gt;

    &lt;bean id="instantiationAwareBeanPostProcessor" class="springBeanTest.MyInstantiationAwareBeanPostProcessor"&gt;
    &lt;/bean&gt;

    &lt;bean id="beanFactoryPostProcessor" class="springBeanTest.MyBeanFactoryPostProcessor"&gt;
    &lt;/bean&gt;
    
    &lt;bean id="person" class="springBeanTest.Person" init-method="myInit"
        destroy-method="myDestory" scope="singleton" p:name="张三" p:address="深圳"
        p:phone="10086" /&gt;

&lt;/beans&gt;</code></pre>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">6. 测试</h3>
                        <div class="code-block">
                            <div class="code-header">
                                <span class="text-gray-400 text-sm">Java</span>
                                <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>@Test
public void test1(){
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Person person = ctx.getBean("person", Person.class);
    System.out.println(person);
    ctx.close();
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Source Code Demonstration -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8">
                <div class="flex items-center mb-6">
                    <div class="step-icon">
                        <i class="fas fa-code-branch"></i>
                    </div>
                    <h2 class="text-3xl font-bold text-gray-800">SpringBean生命周期源码演示</h2>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <span class="text-gray-400 text-sm">Java</span>
                        <button class="text-gray-400 hover:text-white"><i class="far fa-copy"></i></button>
                    </div>
                    <pre class="p-4 overflow-x-auto"><code>public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        // 准备，记录容器的启动时间startupDate, 标记容器为激活，初始化上下文环境如文件路径信息，验证必填属性是否填写
        prepareRefresh();

        // 获取新的beanFactory，销毁原有beanFactory、为每个bean生成BeanDefinition等
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // 初始化beanFactory的各种属性
        prepareBeanFactory(beanFactory);

        try {
            // 模板方法，此时，所有的beanDefinition已经加载，但是还没有实例化。
            //允许在子类中对beanFactory进行扩展处理。比如添加ware相关接口自动装配设置，添加后置处理器等，是子类扩展prepareBeanFactory(beanFactory)的方法
            postProcessBeanFactory(beanFactory);

            // 实例化并调用所有注册的beanFactory后置处理器（实现接口BeanFactoryPostProcessor的bean，在beanFactory标准初始化之后执行）
            invokeBeanFactoryPostProcessors(beanFactory);

            //注册bean后置处理器
            registerBeanPostProcessors(beanFactory);

            // 初始化上下文的消息
            initMessageSource();

            // 初始化事件
            initApplicationEventMulticaster();

            // 模板方法，在容器刷新的时候可以自定义逻辑，不同的Spring容器做不同的事情。
            onRefresh();

            // 注册监听器，广播early application events
            registerListeners();

            // 实例化所有剩余的（非懒加载）单例
            // 比如invokeBeanFactoryPostProcessors方法中根据各种注解解析出来的类，在这个时候都会被初始化。
            // 实例化的过程各种BeanPostProcessor开始起作用。
            finishBeanFactoryInitialization(beanFactory);

            // refresh做完之后需要做的其他事情。
            // 清除上下文资源缓存（如扫描中的ASM元数据）
            // 初始化上下文的生命周期处理器，并刷新（找出Spring容器中实现了Lifecycle接口的bean并执行start()方法）。
            // 发布ContextRefreshedEvent事件告知对应的ApplicationListener进行响应的操作
            finishRefresh();
        }

        catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                        "cancelling refresh attempt: " + ex);
            }

            // 销毁已经生成的bean
            destroyBeans();

            // 重置激活状态.
            cancelRefresh(ex);

            // Propagate exception to caller.
            throw ex;
        }

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}</code></pre>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                    <p class="text-gray-400 mt-2">探索技术的无限可能</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```